package com.schoolai.schoolset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.schoolai.entity.SchoolSetAfterSchoolServiceOrder;
import com.schoolai.schoolset.mapper.SchoolSetAfterSchoolServiceMapper;
import com.schoolai.schoolset.mapper.SchoolSetAfterSchoolServiceOrderMapper;
import com.schoolai.schoolset.req.SetAfterSchoolServiceREQ;
import com.schoolai.schoolset.service.ISchoolSetAfterSchoolServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolai.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.schoolai.entity.SchoolSetAfterSchoolService;
import io.micrometer.common.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bobby
 * @since 2025-11-06
 */
@Service
public class SchoolSetAfterSchoolServiceServiceImpl extends ServiceImpl<SchoolSetAfterSchoolServiceMapper, SchoolSetAfterSchoolService> implements ISchoolSetAfterSchoolServiceService {
    @Autowired
    SchoolSetAfterSchoolServiceOrderMapper schoolSetAfterSchoolServiceOrderMapper;
    @Override
    public Result queryPage(SetAfterSchoolServiceREQ req) {
        LambdaQueryWrapper<SchoolSetAfterSchoolService> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(req.getServiceName())){
            wrapper.like(SchoolSetAfterSchoolService::getServiceName, req.getServiceName());
        }
       wrapper.orderByDesc(SchoolSetAfterSchoolService::getId);
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }
    @Transactional
    @Override
    public Result signUpAfterSchoolService(String afterSchoolServiceId, String stuId) {
        if (StringUtils.isNotBlank(afterSchoolServiceId)&&StringUtils.isNotBlank(stuId)){
            SchoolSetAfterSchoolService schoolSetAfterSchoolService = baseMapper.selectById(afterSchoolServiceId);
            if(schoolSetAfterSchoolService==null){
                return Result.error("课后服务不存在！");
            }
            //
            if(schoolSetAfterSchoolService.getQuota()<1)
            {
                // System.out.println("课程已经报满！");
                return Result.ok("课程已经报满！");
            }
            // 已经报名无需重复提交
            Long signUpCount =schoolSetAfterSchoolServiceOrderMapper.selectCount(new LambdaQueryWrapper<SchoolSetAfterSchoolServiceOrder>()
                    .eq(SchoolSetAfterSchoolServiceOrder::getStuId, stuId)
                    .eq(SchoolSetAfterSchoolServiceOrder::getAfterSchoolSetId,afterSchoolServiceId));
            if(signUpCount>0){
                // System.out.println("已经报名无需重复！");
                return Result.ok("已经报名无需重复！");
            }
            // 课后服务名额-1
            schoolSetAfterSchoolService.setQuota(schoolSetAfterSchoolService.getQuota()-1);
            schoolSetAfterSchoolService.setUpdatedTime(new Date());
            baseMapper.insertOrUpdate(schoolSetAfterSchoolService);
            // 添加报名表
            SchoolSetAfterSchoolServiceOrder schoolSetAfterSchoolServiceOrder = new SchoolSetAfterSchoolServiceOrder();
            schoolSetAfterSchoolServiceOrder.setAfterSchoolSetId(afterSchoolServiceId);
            schoolSetAfterSchoolServiceOrder.setStuId(stuId);
            schoolSetAfterSchoolServiceOrder.setCreatedTime(new Date());
            schoolSetAfterSchoolServiceOrderMapper.insert(schoolSetAfterSchoolServiceOrder);
            // System.out.println("课程报名成功！");
        }
        return Result.ok();
    }
}
